package com.augurit.gzcgwsp.csshlj.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.gzcgwsp.csshlj.dao.PersonDao;
import com.augurit.gzcgwsp.csshlj.entity.Person;
import com.augurit.gzcgwsp.csshlj.service.IPersonService;
import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.augurit.gzcgwsp.excelimportexport.util.GetFileName;

/**
 *城市生活垃圾处置专业人员service实现类
 * @author Administrator
 *
 */
@Service
@Transactional
public class PersonServiceImpl implements IPersonService{
	private static final Logger logger=Logger.getLogger(PersonServiceImpl.class);
	@Autowired
	private PersonDao personDao;
	/*
	 * 分页查询(non-Javadoc)
	 * @see com.augurit.gzcgwsp.csshlj.service.IPersonService#toListPerson(org.springside.modules.orm.Page, java.lang.Long)
	 */
	@Transactional(readOnly = true)
	public Page toListPerson(Page page, Long id) {
		String sql=" from "+Person.class.getName()+" where czId="+id;
		List<PropertyFilter> filterList = new ArrayList<PropertyFilter>();
//		PropertyFilter filter = new PropertyFilter("id_", String.valueOf(id));
//		filterList.add(filter);
//		//设置默认排序方式
//		if (!page.isOrderBySetted()) {
//			page.setOrderBy("id");
//			page.setOrder(Page.ASC);
//		}
		return personDao.findPage(page, sql,new ArrayList());
		//return personDao.findPage(page, filterList);
	}
	
	/**
	 * 
	 */
	public boolean saveOrUpdatePerson(Map map,File jlfile,File xlfile) {
		String name=(String) map.get("name");
		String xl=(String) map.get("xl");
		String zy=(String) map.get("zy");
		String id=(String) map.get("personId");
		String jlFileName=(String) map.get("jlFileName");
		String xlFileName=(String) map.get("xlFileName");
		String contextPath=(String) map.get("contextPath");
		String czid=(String) map.get("czid");
		if(StringUtils.isNotBlank(czid)){
			if(StringUtils.isBlank(id)){
				Person person=new Person();
				person.setName(name);
				person.setXl(xl);
				person.setZy(zy);
				person.setCzid(Long.valueOf(czid));
				this.uploadFile(jlfile, xlfile, jlFileName,
						xlFileName, contextPath, person);
				this.personDao.save(person);
			}
		}
	
		return false;
	}
	
	/**
	 * 上传证书
	 * @param jlfile
	 * @param xlfile
	 */
	private void uploadFile(File jlfile,File xlfile,String jlFileName,
			String xlFileName,String contextPath,Person person){
		String jlpath="Templates/outFile/csshljcz/zyry/jl";
		String xlpath="Templates/outFile/csshljcz/zyry/xl";
		if(jlfile!=null){
			jlFileName=jlFileName.substring(jlFileName.lastIndexOf(".")+1,jlFileName.length());//文件后缀名称
			String jlRealpath=GetFileName.GetRealPath(jlpath,jlFileName);//路径加年月组成
			person.setJlfilepath(jlRealpath);
			jlRealpath=jlRealpath.replace("/", File.separator);
			FileUtil.uploadFile(contextPath, jlRealpath, jlfile);
			
		}
		if(xlfile!=null){
			xlFileName=xlFileName.substring(xlFileName.lastIndexOf(".")+1,xlFileName.length());
			String xlRealpath=GetFileName.GetRealPath(xlpath,xlFileName);
			person.setXlfilepath(xlRealpath);
			xlRealpath=xlRealpath.replace("/", File.separator);
			FileUtil.uploadFile(contextPath, xlRealpath, xlfile);
			
		}
		
	}

	/**
	 * 查询实体
	 */
	public Person getEntity(Long personId) {
			String sql="from "+Person.class.getName()+" where id="+Long.valueOf(personId);
			return (Person) personDao.find(sql).get(0);

	}

	public void updateEntity(String contextPath, Person person,
			InputStream jlfile, InputStream xlfile,String jlfileName,String xlfileName) {
		String jlpath="Templates/outFile/csshljcz/zyry/jl";
		String xlpath="Templates/outFile/csshljcz/zyry/xl";
		try {
			if(jlfile!=null && jlfile.available()>0){
				String jlRealpath=GetFileName.GetRealPath(jlpath,jlfileName);//路径加年月组成
				person.setJlfilepath(jlRealpath);
				String filepath=contextPath+jlRealpath;
				this.uploadFileByStream(filepath, contextPath, jlfile);
			}
			if(xlfile!=null && xlfile.available()>0){
				String xlRealpath=GetFileName.GetRealPath(xlpath,xlfileName);//路径加年月组成
				person.setJlfilepath(xlRealpath);
				String filepath=contextPath+xlRealpath;
				filepath=filepath.replace("/", File.separator);
				this.uploadFileByStream(filepath, contextPath, xlfile);
			}
			this.personDao.save(person);
		} catch (Exception e) {
			logger.info("修改错误"+e);
		} 
	}

	/**
	 * 有流的情况上传文件
	 * @param filepath
	 * @param inpuStream
	 */
	private void uploadFileByStream(String filepath,String contextPath,InputStream fileStream){
		try {
			File file=new File(filepath);
			if(!file.exists()){
				String dirPaths=filepath.substring(0,filepath.lastIndexOf(File.separator));
				File dir=new File(dirPaths);
				if(!dir.exists()){
					FileUtil.createDir(dirPaths);
				}
				file.createNewFile();
			}
			FileOutputStream targetFile = new FileOutputStream(new File(
					(filepath).replace("/", File.separator)));
			byte[] size = new byte[fileStream.available()];
			fileStream.read(size);
			targetFile.write(size);
			targetFile.flush();
			targetFile.close();
			fileStream.close();
		} catch (Exception e) {
			logger.info("修改错误"+e);
		} 
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.csshlj.service.IPersonService#deleteById(java.lang.Long)
	 */
	public boolean deleteById(Long personId) {
		boolean flag=false;
		Person person=this.getEntity(personId);
		try {
			this.personDao.delete(person);
			flag=true;
		} catch (Exception e) {
		
		}
		return flag;
	}
	
	

}
